<?php

namespace Home\Service;

use Home\Model\EcmsNewsModel;
use Home\Model\EnewsclassModel;

class EcmsNewsService
{
    /**
     * 下拉页面返回请求数据
     * @param $getData get传参数据
     */
    public function getListForClass($getData)
    {
        $cat = $getData['cat']; //分类的id
        $page = $getData['page'] ? $getData['page'] : 1; //当前要取的分页
        $url = U('index/pushDownList',['cat'=>$cat,'page'=>$page+1]);//获取数据的url
        //数据分页
        $rows = C('PAGE_SIZE'); //每次显示的条数
        $start = $page ? (intval($page) - 1) * $rows : 0;
        $limit = "{$start},{$rows}";

        $fields = 'id,titleurl,title,titlepic,smalltext';
        $newModel = new EcmsNewsModel();
        $cat = $this-> getChildClassIdArr($cat);
        $condition = ['classid' => ['in',$cat]];
        $order = "lastdotime desc";
        $result = $newModel -> getSelNews($condition,$fields,$limit,$order);
        //存在数据的时候进行拼装
        $html = '';
        if($result['status'] == 1)
        {
            foreach ($result['data'] as $k => $v)
            {

                if($v['titlepic']){
                    $html .= "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">
                    <div class=\"am-u-sm-4 am-list-thumb\">
                        <a href=\"".$v['titleurl']."\">
                            <img src=\"".'http://www.haotoufa.com'. str_replace("http://www.haotoufa.com", "", $v['titlepic']) ."\" />
                        </a>
                    </div>
                    <div class=\"am-u-sm-8 am-list-main\">
                        <h3 class=\"am-list-item-hd\">
                            <a href=\"".$v['titleurl']."\">".$v['title']."</a>
                        </h3>
                        <div class=\"am-list-item-text\">".html_entity_decode($v['smalltext'])."</div>
                    </div>
                </li>";

                }else{
                    $html .= '<li class="am-g am-list-item-desced">

                    <div class=" am-list-main">
                        <h3 class="am-list-item-hd"><a href="'. $v['titleurl'] .'" class="" >' . $v['title'] . '</a></h3>

                        <div class="am-list-item-text">'. html_entity_decode($v['smalltext']).'</div>

                    </div>
                  </li>';
                }


            }
            $html .= "<div class=\"am-g am-text-center\" id=\"next-page\"
                url=\"".$url."\"><i class=\"am-icon-arrow-circle-o-down\"></i>&nbsp; 加载中
                <i class=\"am-icon-spinner am-icon-spin\"></i>...";
            $html .= "<script type=\"text/javascript\">
                        $('#next-page').scrollspy({
                            animation: 'slide-top',
                            repeat: false
                        });
                        $('#next-page').on('inview.scrollspy.amui', function() {
                            var url = $(this).attr('url');
                            if('end' !== url){
                                $.ajax({
                                    beforeSend: function(){
                                        return true;
                                    },
                                    complete: function(){
                                        return true;
                                    },
                                    url: $(this).attr('url'),
                                    dataType: \"html\",
                                    type: \"POST\",
                                    cache: false,
                                    async:true,
                                    success: function(html){
                                        $('#next-page').remove();
                                        $('#newContent').append(html)
                                    }
                                });
                            }
                        });
                    </script></div>";
        }
        else
        {
            $html .= "<hr class=\"am-margin-top-sm am-margin-bottom-sm\">
                    <div class=\"am-g am-text-center\" id=\"next-page\" url=\"end\"> 没有更多啦....</div><script type=\"text/javascript\">
                        $('#next-page').scrollspy({
                            animation: 'slide-top',
                            repeat: false
                        });
                        $('#next-page').on('inview.scrollspy.amui', function() {
                            var url = $(this).attr('url');
                            if('end' !== url){
                                $.ajax({
                                    beforeSend: function(){
                                        return true;
                                    },
                                    complete: function(){
                                        return true;
                                    },
                                    url: $(this).attr('url'),
                                    dataType: \"html\",
                                    type: \"POST\",
                                    cache: false,
                                    async:true,
                                    success: function(html){
                                        $('#next-page').remove();
                                        $('#newContent').append(html)
                                    }
                                });
                            }
                        });
                    </script>";
        }
        echo $html;
    }

    public function getListOrderByTime($getData){

        $page = $getData['page'] ? $getData['page'] : 1; //当前要取的分页
        $url = '/Index/slideUp/page/' . ($page+1); //获取数据的url
        //数据分页
        $rows = C('PAGE_SIZE'); //每次显示的条数
        $start = $page ? (intval($page) - 1) * $rows : 0;
        $limit = "{$start},{$rows}";

        $newModel = new EcmsNewsModel();


        $fields = 'id,titleurl,title,titlepic,smalltext';
        $order = "lastdotime desc";
        $result = $newModel -> getNewsOrderByTime($fields, $limit, $order);

        //存在数据的时候进行拼装
        $html = '';
        if($result)
        {
            foreach ($result as $k => $v)
            {

                if($v['titlepic']){
                    $html .= "<li class=\"am-g am-list-item-desced am-list-item-thumbed am-list-item-thumb-left\">
                    <div class=\"am-u-sm-4 am-list-thumb\">
                        <a href=\"".$v['titleurl']."\">
                            <img src=\"".'http://www.haotoufa.com'. str_replace("http://www.haotoufa.com", "", $v['titlepic']) ."\" />
                        </a>
                    </div>
                    <div class=\"am-u-sm-8 am-list-main\">
                        <h3 class=\"am-list-item-hd\">
                            <a href=\"".$v['titleurl']."\">".$v['title']."</a>
                        </h3>
                        <div class=\"am-list-item-text\">".html_entity_decode($v['smalltext'])."</div>
                    </div>
                </li>";

                }else{
                    $html .= '<li class="am-g am-list-item-desced">

                    <div class=" am-list-main">
                        <h3 class="am-list-item-hd"><a href="'. $v['titleurl'] .'" class="" >' . $v['title'] . '</a></h3>

                        <div class="am-list-item-text">'. html_entity_decode($v['smalltext']).'</div>

                    </div>
                  </li>';
                }


            }
            $html .= "<div class=\"am-g am-text-center\" id=\"next-page\"
                url=\"".$url."\"><i class=\"am-icon-arrow-circle-o-down\"></i>&nbsp; 加载中
                <i class=\"am-icon-spinner am-icon-spin\"></i>...";
            $html .= "<script type=\"text/javascript\">
                        $('#next-page').scrollspy({
                            animation: 'slide-top',
                            repeat: false
                        });
                        $('#next-page').on('inview.scrollspy.amui', function() {
                            var url = $(this).attr('url');
                            if('end' !== url){
                                $.ajax({
                                    beforeSend: function(){
                                        return true;
                                    },
                                    complete: function(){
                                        return true;
                                    },
                                    url: $(this).attr('url'),
                                    dataType: \"html\",
                                    type: \"POST\",
                                    cache: false,
                                    async:true,
                                    success: function(html){
                                        $('#next-page').remove();
                                        $('#newContent').append(html)
                                    }
                                });
                            }
                        });
                    </script></div>";
        }
        else
        {
            $html .= "<hr class=\"am-margin-top-sm am-margin-bottom-sm\">
                    <div class=\"am-g am-text-center\" id=\"next-page\" url=\"end\"> 没有更多啦....</div><script type=\"text/javascript\">
                        $('#next-page').scrollspy({
                            animation: 'slide-top',
                            repeat: false
                        });
                        $('#next-page').on('inview.scrollspy.amui', function() {
                            var url = $(this).attr('url');
                            if('end' !== url){
                                $.ajax({
                                    beforeSend: function(){
                                        return true;
                                    },
                                    complete: function(){
                                        return true;
                                    },
                                    url: $(this).attr('url'),
                                    dataType: \"html\",
                                    type: \"POST\",
                                    cache: false,
                                    async:true,
                                    success: function(html){
                                        $('#next-page').remove();
                                        $('#newContent').append(html)
                                    }
                                });
                            }
                        });
                    </script>";
        }

        echo $html;
    }

    /**
     * 获取当前分类下的子类id,不存在子类id直接返回当前的id
     * @param $classId
     * @return array|mixed
     */
    public function getChildClassIdArr($classId)
    {
        if(!$classId)
        {
            $this->error('参数传递错误');
        }
        $newClassModel = new EnewsclassModel();
        $condition = [
            'classid' => $classId
        ];
        $fields = 'classid,sonclass';
        
        $classData = $newClassModel -> getLists($fields,$condition);
        $sonClassStr = $classData[0]['sonclass'];
        if($sonClassStr)
        {
            //现在栏目分类最多3层，直接进行判断一次子类是否存在，获取没有子类的栏目id，返回
            $sonClassArr = explode('|',$sonClassStr);
            unset($sonClassArr[0],$sonClassArr[count($sonClassArr)]);
            $newClassStr = implode(',',$sonClassArr);
        }

        return $newClassStr ? $newClassStr : $classId;
    }

    /**
     * 面包屑数据
     * @param $classId
     * @return array
     * create by wenQing
     */
    public function getBreadData($classId){

        $newClassModel = new EnewsclassModel();
        $condition = [
            'classid' => $classId
        ];
        $fields = 'featherclass';

        $tmp = $newClassModel->where($condition)->getField($fields);

        if(!empty($tmp)){
            $tmp = ltrim($tmp, '|');
            $tmp = rtrim($tmp, '|');

            //进行分割
            $tmpArr = explode('|', $tmp);
            foreach($tmpArr as $k => $v){
                //取得对应的 classname
                $data[] = $newClassModel->field('classid, classname')->where('classid=' . $v)->find();
            }
        }else{
           $data[] = $newClassModel->field('classid, classname')->where('classid=' . $classId)->find();
        }


        return $data;
    }
}